/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Reflection;
using System.Web;
using System.Web.Caching;
using System.Web.UI.WebControls;
using EPiServer.Municipality.Web.UI.WebControls;
using EPiServer.PlugIn;

namespace EPiServer.Municipality.Configuration {
    /// <summary>
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    [GuiPlugIn(Area = PlugInArea.None, DisplayName = "Configuration for EPiServer Municipality")]
    public class SiteSettings {
        internal const string SiteConfigurationCacheKey = "ConfigurationCacheKey";
        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region public string WebmasterEmail
        /// <summary>
        /// Get/Sets the WebMasterEmail of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Email address to the web master", AdminControl = typeof(TextBox))]
        public string WebmasterEmail { get; set; }
        #endregion
        #region public string SystemEmail
        /// <summary>
        /// Get/Sets the SystemEmail of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Sender email address used when the system sends email", AdminControl = typeof(TextBox))]
        public string SystemEmail { get; set; }
        #endregion
        #region public int ContactPageTypeId
        /// Get/Sets the ContactPageTypeId of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Contact card page type", AdminControl = typeof(InputPageType), AdminControlValue = "PageTypeId")]
        public int ContactPageTypeId { get; set; }
        #endregion
        #region public string NotifierDivider
        /// <summary>
        /// Get/Sets the NotifierDivider of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "<br /><b>Scheduled notification for new or update pages</b>", AdminControl = typeof(Literal))]
        public string NotifierDivider { get; set; }
        #endregion
        #region public string RootPages
        /// <summary>
        /// Get/Sets the RootPages of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Root pages (comma separated)", AdminControl = typeof(TextBox))]
        public string RootPages { get; set; }
        #endregion
        #region public string Receiver
        /// <summary>
        /// Get/Sets the Receiver of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Email address to receiver of notification email", AdminControl = typeof(TextBox))]
        public string Receiver { get; set; }
        #endregion
        #region public string Sender
        /// <summary>
        /// Get/Sets the Sender of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Sender email address for notification email", AdminControl = typeof(TextBox))]
        public string Sender { get; set; }
        #endregion
        #region public string IncludedPageTypes
        /// <summary>
        /// Get/Sets the IncludedPageTypes of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Page type id:s on pages that are included in notification emails", AdminControl = typeof(TextBox))]
        public string IncludedPageTypes { get; set; }
        #endregion
        #region public int ChangedSince
        /// <summary>
        /// Get/Sets the ChangedSince of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Send update page notification for pages older than (in days)", AdminControl = typeof(TextBox))]
        public int ChangedSince { get; set; }
        #endregion
        #region public string LanguageId
        /// <summary>
        /// Get/Sets the LanguageId of the SiteSettings
        /// </summary>
        /// <value></value>
        [PlugInProperty(Description = "Language used in notification emails", AdminControl = typeof(TextBox))]
        public string LanguageId { get; set; }
        #endregion
        #region public static SiteSettings Instance
        /// <summary>
        /// Gets the Instance of the SiteSettings
        /// </summary>
        /// <value></value>
        public static SiteSettings Instance {
            get {
                if (HttpContext.Current.Cache[SiteConfigurationCacheKey] != null)
                    return (SiteSettings)HttpContext.Current.Cache[SiteConfigurationCacheKey];

                var config = new SiteSettings();
                PlugInSettings.AutoPopulate(config);
                HttpContext.Current.Cache.Add(SiteConfigurationCacheKey, config, null, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(8), CacheItemPriority.Normal, null);
                return config;
            }
        }
        #endregion
        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
    }

    public class ConfigurationCacheManager : PlugInAttribute {
        /* *******************************************************************
       *  Methods 
       * *******************************************************************/
        #region public static void Start()
        /// <summary>
        /// 
        /// </summary>
        public static void Start() {
            PlugInSettings.SettingsChanged += RemoveCachedSettings;
        }
        #endregion
        #region private static void RemoveCachedSettings(object sender, EventArgs e)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void RemoveCachedSettings(object sender, EventArgs e) {
            var plugIn = sender as PlugInDescriptor;
            string executingAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;

            if (plugIn != null && plugIn.AssemblyName.Equals(executingAssemblyName))
                HttpContext.Current.Cache.Remove(SiteSettings.SiteConfigurationCacheKey);

        }
        #endregion
    }
}